package com.atguigu.day21.obj04.exer;

import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/26 16:06
 * @注释
 */
public class EmployeeStreat {
    @Test
    public void test02(){
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\list.txt"))) {
            ArrayList<Employee> list = (ArrayList<Employee>) ois.readObject();
            list.forEach(System.out::println);
            //System.out.println(list);
        } catch (ClassNotFoundException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test01(){
        ArrayList<Employee> list = new ArrayList<Employee>();
        Employee emp1 = new Employee("张三",20);
        Employee emp2 = new Employee("李四",19);
        Employee emp3 = new Employee("王五",30);
        Employee emp4 = new Employee("赵六",21);
        Employee emp5 = new Employee("陈一",25);

        Collections.addAll(list, emp1, emp2, emp3, emp4, emp5);

        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\list.txt"))) {
            oos.writeObject(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
